package com.haisen.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.haisen.core.utils.DateUtil;
import com.haisen.dataobject.entity.DingReport;
import com.haisen.service.DingReportService;
import com.haisen.service.DingTokenServcie;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

//todo:这个类没有进行测试，上线时再投入使用
@Service
@Slf4j
public class DingReportServiceImpl implements DingReportService {


    private String accessToken = "";

    @Autowired
    private DingTokenServcie dingTokenServcie;

    StringBuilder sb = new StringBuilder();

    @Override
    public OapiReportListResponse list(Long start_time, Long end_time, String template_name, String userid,Long cursor) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/list");
        OapiReportListRequest request = new OapiReportListRequest();
        request.setTemplateName(template_name);
         if (!StringUtils.isEmpty(userid)){
             request.setUserid(userid);
         }
        request.setStartTime(start_time);
        request.setEndTime(end_time);
        request.setCursor(cursor);
        request.setSize(10L);
        OapiReportListResponse response = null;
        try {
             response = client.execute(request, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 获取汇总后的外事信息
     * @param start_time
     * @param end_time
     * @param template_name
     * @param userId
     * @param cursor
     * @return
     */
    public String getreportTail(Long start_time,Long end_time,String template_name,String userId,Long cursor){
        OapiReportListResponse reportListResponse=  list(start_time,end_time,template_name,userId,cursor);
        //获取成功
        if (reportListResponse.isSuccess()){
            OapiReportListResponse.PageVo pageVo = reportListResponse.getResult();

            List<OapiReportListResponse.ReportOapiVo> dataList = pageVo.getDataList();
            for (OapiReportListResponse.ReportOapiVo reportOapiVo : dataList){
                String deptName = reportOapiVo.getDeptName();
                sb.append("部门："+deptName+"\n");
                String creatorName = reportOapiVo.getCreatorName();
                sb.append("提交人："+creatorName+"\n");
                Long creattime = reportOapiVo.getCreateTime();
                String creatdate = DateUtil.toDateTimeString(new Date(creattime),"yyyy-MM-dd HH:mm");
                sb.append("提交时间："+creatdate+"\n");
                List<OapiReportListResponse.JsonObject>  jsonObjectList =   reportOapiVo.getContents();
                for (OapiReportListResponse.JsonObject jsonObject : jsonObjectList){
                    sb.append(jsonObject.getKey()+":"+ jsonObject.getValue()+"\n");
                }
              //  log.info("创建时间：{},创建部门：{},创建人：{},内容={}",creatdate,deptName,creatorName,sb.toString());
                sb.append("\n");
                //sb.delete(0, sb.length());
            }
            //有分页，递归
            if(pageVo.getHasMore()){
                Long next_cursor = pageVo.getNextCursor();
                getreportTail(start_time,end_time,template_name,userId,next_cursor);
            }
        }
      return sb.toString();
    }


    @Override
    public OapiReportStatisticsResponse getReportStatistics(String report_id) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/statistics");
        OapiReportStatisticsRequest req = new OapiReportStatisticsRequest();
        req.setReportId("abc");
        OapiReportStatisticsResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
       // System.out.println(rsp.getBody());
        return rsp;
    }

    @Override
    public OapiReportStatisticsListbytypeResponse getReportStatisticsListbytype(String report_id, Number type) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/statistics/listbytype");
        OapiReportStatisticsListbytypeRequest req = new OapiReportStatisticsListbytypeRequest();
        req.setReportId("abc");
        req.setType(0L);
        req.setOffset(0L);
        req.setSize(100L);
        OapiReportStatisticsListbytypeResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        //System.out.println(rsp.getBody());
        return rsp;
    }

    @Override
    public OapiReportReceiverListResponse getReportReceiverList(String report_id) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/receiver/list");
        OapiReportReceiverListRequest req = new OapiReportReceiverListRequest();
        req.setReportId("abc");
        req.setOffset(0L);
        req.setSize(100L);
        OapiReportReceiverListResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
        return rsp;
    }

    @Override
    public OapiReportCommentListResponse getReportCommentList(String report_id) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/comment/list");
        OapiReportCommentListRequest req = new OapiReportCommentListRequest();
        req.setReportId("abc");
        req.setOffset(0L);
        req.setSize(20L);
        OapiReportCommentListResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
        return rsp;
    }

    @Override
    public OapiReportGetunreadcountResponse getReportGetunreadcount(String userid) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/getunreadcount");
        OapiReportGetunreadcountRequest request = new OapiReportGetunreadcountRequest();
        request.setUserid("1226682231742708");
        OapiReportGetunreadcountResponse response = null;
        try {
            response = client.execute(request,accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public OapiReportTemplateListbyuseridResponse getReportTemplateListbyuserid(String userid) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/report/template/listbyuserid");
        OapiReportTemplateListbyuseridRequest req = new OapiReportTemplateListbyuseridRequest();
        req.setUserid(userid);
        req.setOffset(0L);
        req.setSize(100L);
        OapiReportTemplateListbyuseridResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp;
    }

    @Override
    public void create(DingReport dingReport) {

    }

    @Override
    public List<DingReport> listAll() {
        return null;
    }
}
